package com.czy.daishusys.utils;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;

import com.czy.daishusys.R;
import com.tbruyelle.rxpermissions2.RxPermissions;

import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;

/**
 * Created by hedepu on 2017-09-04
 */

public class IntentUtils {
    public static void onClickCallPhone(final Context content, final String phone) {
        if (PhoneUtils.isEmpty(phone)) {
            PhoneUtils.ShowToastMessage(content, "未获取到联系方式");
            return;
        }
        RxPermissions rxPermissions = new RxPermissions((Activity) content);
        rxPermissions.request(Manifest.permission.CALL_PHONE)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(@NonNull Boolean aBoolean) throws Exception {
                        if (aBoolean) {
                            ExitLoginDialog(content, phone, "呼叫", "取消");

                        } else {
                            PhoneUtils.ShowToastMessage(content, "获取手机状态权限拒绝");

                        }
                    }
                });

    }

    private static void ExitLoginDialog(final Context content, final String title, String sure, String cancle) {
        /**有标题，标题图标，信息，取消和确定按钮*/
        PicDialog.Builder builder = new PicDialog.Builder(content);
        builder.setTitleIcon(R.mipmap.ic_btn_call)
//                .setTitle(title)
                .setMessage("确定拨打" + title + "?")
                .setNegativeIcon(R.mipmap.ic_dialog_cancel)
                .setPositiveIcon(R.mipmap.ic_dialog_ensure)

                .setNegativeButton(cancle, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                    }
                })
                .setPositiveButton(sure, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Intent intent = new Intent();
                        intent.setAction(Intent.ACTION_CALL);
                        intent.setData(Uri.parse("tel:" + title));
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        content.startActivity(intent);
                    }
                });
        builder.create().show();

    }


}
